import swaggerJSDoc from 'swagger-jsdoc'
import { join } from 'path'
import KoaRouter from 'koa-router'

const swaggerRouter = new KoaRouter()

const swaggerDefinition = {
  info: {
    title: 'koa2学习项目',
    version: '1.0.0',
    description: 'API',
  },
  host: 'localhost:3000',
  basePath: '/', // Base path (optional)
}
const options = {
  swaggerDefinition,
  apis: [join(__dirname, './controller/*.ts')], // 写有注解的router的存放地址, 最好path.join()
}
const swaggerSpec = swaggerJSDoc(options)
// 通过路由获取生成的注解文件
swaggerRouter.get('/swagger.json', async function (ctx) {
  ctx.set('Content-Type', 'application/json')
  ctx.body = swaggerSpec
})
export default swaggerRouter
